package com.webinsky.article.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.article.persist.entity.ArticleRelate;

public interface ArticleRelateMapper {
	
	@Delete({
		"delete from article_relate where article_id = #{articleId, jdbcType=INTEGER}"
	})
	int deleteByArticleId(@Param("articleId")int articleId);
	
	 @Insert({
		"insert into article_relate (article_id, relate_article_id, sort, del, create_at, update_at, language)",
		"values (#{articleId,jdbcType=INTEGER}, #{relateArticleId,jdbcType=INTEGER}, #{sort,jdbcType=INTEGER}, #{del,jdbcType=INTEGER}, ",
		"now(), now(), #{language,jdbcType=VARCHAR})"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(ArticleRelate record);
	    
	@Select({
	    "select",
	    "id, article_id, relate_article_id, sort, del, create_at, update_at, language",
	    "from article_relate"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="relate_article_id", property="relateArticleId", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleRelate> selectAll();
	
	@Select({
	    "select",
	    "id, article_id, relate_article_id, sort, del, create_at, update_at, language",
	    "from article_relate",
	    "where article_id = #{articleId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="relate_article_id", property="relateArticleId", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleRelate> selectByArticleId(@Param("articleId")int articleId, @Param("del")int del);
	
	@Select({
	    "select",
	    "id, article_id, relate_article_id, sort, del, create_at, update_at, language",
	    "from article_relate ",
	    "where id = #{id, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="relate_article_id", property="relateArticleId", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	ArticleRelate selectById(@Param("id")int id);
	
	@SelectProvider(type = ArticleRelateMapperProvider.class, method = "selectByArticleIds")
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="relate_article_id", property="relateArticleId", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleRelate> selectByArticleIds(@Param("articleIds")List<Integer> ids);
	
	@Select({
		"select",
	    "id, article_id, relate_article_id, sort, del, create_at, update_at, language",
	    "from article_relate ",
	    "where article_id = #{articleId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="relate_article_id", property="relateArticleId", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleRelate> selectByArticleIdAndDel(@Param("articleId")int articleId, @Param("del")int del);
	
	@Update({
	     "update article_relate",
	     "set article_id = #{articleId,jdbcType=INTEGER},",
	     "relate_article_id = #{relateArticleId,jdbcType=VARCHAR},",
	     "sort = #{sort,jdbcType=INTEGER},",
	     "del = #{del,jdbcType=INTEGER},",	
	     "create_at = #{createAt, jdbcType=TIMESTAMP},",
	     "update_at = #{updateAt,jdbcType=TIMESTAMP},",
	     "language = #{language,jdbcType=VARCHAR}",
	     "where Id = #{id,jdbcType=INTEGER}"
	 })
	 int updateByPrimaryKey(ArticleRelate record);
}
